package com.kgcx.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GaodeApi
{
    public static final Logger logger = LoggerFactory.getLogger(GaodeApi.class);

    @Value("${kgcx.gaode.key:7dd39ee9494f8a1e95c56b319ac6ea48}")
    private String key;

    @Value("${kgcx.gaode.directionurl:http://restapi.amap.com/v3/direction/driving}")
    private String directionurl;

    @Value("${kgcx.gaode.geocodeurl:http://restapi.amap.com/v3/geocode/regeo}")
    private String geocodeurl;

    @Value("${kgcx.gaode.distanceurl:http://restapi.amap.com/v3/distance}")
    private String distance;

    /**
     * 高德路径规划调用
     *
     * @param  origin 起点
     * @param  destination 终点
     * @param  strategy 路径计算模式
     * @return
     */
    public Map<String, String> getGaodeRestapi(String origin, String destination, int strategy) {
		/* 调取高德路径规划接口获取规划路线里程 */
        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("origin", origin);
        map.put("destination", destination);
        map.put("strategy", strategy);

        Map<String, String> resultMap = new HashMap<>();

        try
        {
            String sendRequestByGet = HttpRequestUtil.sendRequestByGet(directionurl, map, true);
            JSONObject gaodeJsonObject = JSONObject.parseObject(sendRequestByGet);

            if (!gaodeJsonObject.get("infocode").equals("10000"))
            {
                resultMap.put("msg", gaodeJsonObject.get("info").toString());
                return resultMap;
            }
            else
            {
                /** 获取paths驾车换乘方案 */
                JSONArray pathArr = gaodeJsonObject.getJSONObject("route").getJSONArray("paths");
                /** 获取paths驾车换乘方案的第一条最优行程方案的详细信息 */
                JSONObject pathJsonObject = pathArr.getJSONObject(0);

                /** 获取到规划的里程长度(单位为米) */
                String distance = pathJsonObject.getString("distance");
                /** 获取到规划的里程的行驶时间(单位为秒) */
                String duration = pathJsonObject.get("duration").toString();

                resultMap.put("distance", distance);
                resultMap.put("duration", duration);
                resultMap.put("msg", gaodeJsonObject.get("info").toString());
            }

            return resultMap;
        }
        catch (IOException e)
        {
            logger.error("高德路径规划调用报错：" +  e.getMessage());
            resultMap.put("msg", e.getMessage());
            return resultMap;
        }
    }

    /**
     * 高德逆地理编码调用
     *
     * @param location 地理位置经纬度信息
     * @return
     */
    public List<Map<String, String>> geocode(String location)
    {
        /* 调取高德逆地理编码 */
        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("location", location);
        map.put("batch", true);

        List<Map<String, String>> resultListMap = new ArrayList<Map<String, String>>();

        try
        {
            String sendRequestByGet = HttpRequestUtil.sendRequestByGet(geocodeurl, map, true);
            JSONObject gaodeJsonObject = JSONObject.parseObject(sendRequestByGet);

            if (!gaodeJsonObject.get("infocode").equals("10000"))
            {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("msg", gaodeJsonObject.get("info").toString());
                resultListMap.add(resultMap);

                return resultListMap;
            }
            else
            {
                JSONArray gaodeJsonArray = JSONArray.parseArray(gaodeJsonObject.get("regeocodes").toString());

                for(int i = 0; i < gaodeJsonArray.size(); i++)
                {
                    Map<String, String> resultMap = new HashMap<>();
                    JSONObject jsonObject = gaodeJsonArray.getJSONObject(i);
                    resultMap.put("citycode", jsonObject.getJSONObject("addressComponent").getString("citycode"));

                    resultListMap.add(resultMap);
                }
            }
        }
        catch (IOException e)
        {
            logger.error("高德逆地理编码调用报错：" +  e.getMessage());
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("msg", e.getMessage());
            resultListMap.add(resultMap);
        }

        return resultListMap;
    }

    /**
     * 高德距离计算调用
     *
     * @param  origins 起点
     * @param  destination 终点
     * @return
     */
    public Map<String, String>  gaodeDistance(String origins, String destination)
    {
        /* 调取高德距离测量 */
        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("origins", origins);
        map.put("destination", destination);

        Map<String, String> resultMap = new HashMap<>();

        try
        {
            String sendRequestByGet = HttpRequestUtil.sendRequestByGet(distance, map, true);
            JSONObject gaodeJsonObject = JSONObject.parseObject(sendRequestByGet);

            if (!gaodeJsonObject.get("infocode").equals("10000"))
            {
                resultMap.put("msg", gaodeJsonObject.get("info").toString());
                return resultMap;
            }
            else
            {
                JSONArray gaodeJsonArray = JSONArray.parseArray(gaodeJsonObject.get("results").toString());

                JSONObject resultJson = gaodeJsonArray.getJSONObject(0);

                resultMap.put("distance", resultJson.getString("distance"));
                resultMap.put("duration", resultJson.getString("duration"));
                resultMap.put("msg", gaodeJsonObject.get("info").toString());
            }

            return resultMap;
        }
        catch (IOException e)
        {
            logger.error("高德距离计算调用：" +  e.getMessage());
            resultMap.put("msg", e.getMessage());
            return resultMap;
        }
    }
}
